﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;

namespace ExcelJsonParser
{
    class JsonWorkBooks
    {
        private JsonViewWorkBook _jsonViewWorkBook;
        private Application _excelApp;
        List<JsonWorkBookBase> _jsonWorkBookList = new List<JsonWorkBookBase>();
        public JsonViewWorkBook JsonViewWorkBook { get { return _jsonViewWorkBook; } }
        public JsonWorkBooks(Workbooks wbs)
        {
            _excelApp = wbs.Application;
            foreach (Workbook wb in wbs)
            {
                this.Add(wb);
            }
            InitJsonViewWorkBook();
        }

        private void InitJsonViewWorkBook()
        {
            
            Workbook wb = _excelApp.Workbooks.Add();
            JsonViewWorkBook jsonViewWb = new JsonViewWorkBook(wb);
            _jsonViewWorkBook = jsonViewWb;
        }

        //void _excelJsonWorkBook_SheetChange(object Sh, Range Target)
        //{ }

        //void _excelworkBook_BeforeClose(ref bool Cancel)
        //{
        //    _excelJsonWorkBook.Saved = true;
        //    if (_excelApp.Workbooks.Count > 1)
        //    {
        //        Cancel = true;
        //        _excelJsonWorkBook.Windows[1].Visible = CloseJsonWorkBook();
        //    }
        //}

        //private Boolean CloseJsonWorkBook()
        //{
        //    Boolean jsonWorkBookVisiable = false;
        //    Worksheet ws = _excelJsonWorkBook.ActiveSheet;
        //    string jsonString = GetOutputJsonString(ws);
        //    string jsonWorkBookName = ws.Range["B1"].Value;
        //    string jsonSheetName = ws.Range["D1"].Value;
        //    string rangeAddress = ws.Range["F1"].Value;
        //    if (_excelJsonWorkBook.Name.Equals(jsonWorkBookName))
        //    {
        //        jsonWorkBookVisiable = true;
        //    }
        //    foreach (Worksheet workSheet in _excelApp.Workbooks[jsonWorkBookName].Worksheets)
        //    {
        //        if (jsonSheetName.Equals(workSheet.Name))
        //        {
        //            workSheet.Activate();
        //            workSheet.Range[rangeAddress].Value = jsonString;
        //            workSheet.Range[rangeAddress].Select();
        //        }
        //    }
        //    return jsonWorkBookVisiable;
        //}

        

   

        public Boolean TryGetByName(string name, out JsonWorkBook outputJsonWb)
        {
            List<JsonWorkBook> jsonWorkBookToBeDeleted = new List<JsonWorkBook>();
            foreach (JsonWorkBook jsonWb in _jsonWorkBookList)
            {
                if ("".Equals(jsonWb.Name))
                {
                    jsonWorkBookToBeDeleted.Add(jsonWb);
                }
            }
            foreach (JsonWorkBook jsonWb in jsonWorkBookToBeDeleted)
            {
                _jsonWorkBookList.Remove(jsonWb);
            }
            foreach (JsonWorkBook jsonWb in _jsonWorkBookList)
            {
                if (name.Equals(jsonWb.Name))
                {
                    outputJsonWb = jsonWb;
                    return true;
                }
            }
            outputJsonWb = null;
            return false;
        }

        public Boolean Add(Workbook wb)
        {
            JsonWorkBook jsonWb = null;
            if (TryGetByName(wb.Name,out jsonWb) == false)
            {
                jsonWb = new JsonWorkBook(wb);
                _jsonWorkBookList.Add(jsonWb);
                return true;
            }
            return false;
        }

        public Boolean Remove(Workbook wb)
        {
            JsonWorkBook jsonWb = null;
            if (TryGetByName(wb.Name, out jsonWb))
            {
                _jsonWorkBookList.Remove(jsonWb);
                return true;
            }
            return false;
        }

        //public static JsonWorkBooks GetInstance()
        //{
        //    if (_jsonWorkBooks == null)
        //    {
        //        _jsonWorkBooks = new JsonWorkBooks();
        //    }
        //    return _jsonWorkBooks;
        //}
    }
}
